/***************************************************************
 * Name:      main.cpp
 * Purpose:   Delete program from PROGRAM.ALL dump
 * Author:    Riku Maikola (riku@maikola.net)
 * Usage:     DeleteO "program.all" "O????"
 * Created:   2014-05-03 - 2014-05-03
 * Copyright: Riku Maikola
 * License:   GPL
 **************************************************************/

#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>

using namespace std;

int main(int n, char* Args[])
{
    if(n==3)
    {
        string line;
        uint start=0,end=-1,counter=-1;

        vector<string> rows;
        ifstream codefile(Args[1]);
        if (codefile.is_open())
        {
            while(getline(codefile,line))
            {
                counter++;
                rows.push_back(line);
                if(line.find(Args[2])!=std::string::npos)
                {
                    start=counter;
                    end=counter;
                    while(getline(codefile,line))
                    {
                        rows.push_back(line);
                        counter++;
                        end++;
                        if(line[0]==79)break;
                    }
                }
            }
            codefile.close();
            rows.erase(rows.begin()+start,rows.begin()+end);    //erase the space between prorgrams

            //save modified
            ofstream myfile;
            myfile.open(Args[1]);
            if(myfile.is_open() && (rows.size()>0))
            {
                for(uint b=0; b<rows.size(); b++)myfile << rows[b] << "\n";
                myfile.close();
            }
        }
    }
    return 0;
}


